import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login.vue";
import Home from "../views/Home.vue";
import Not from "../views/Not.vue";
import User from "../components/user.vue";
import userList from "../components/userList.vue";
import role from "../components/role.vue";
import roleList from "../components/roleList.vue";
import money from "../components/money.vue";
import moneyList from "../components/moneyList.vue";
import welcomeHome from "../components/welcomeHome.vue";
import store from "../store/index";
Vue.use(VueRouter);

//创建规则
const userRule = { path: "/user", component: User };
const userListRule = { path: "/userList", component: userList };
const roleRule = { path: "/role", component: role };
const roleListRule = { path: "/roleList", component: roleList };
const moneyRule = { path: "/money", component: money };
const moneyListRule = { path: "/moneyList", component: moneyList };

const ruleMapping = {
    user: userRule,
    userList: userListRule,
    role: roleRule,
    roleList: roleListRule,
    money: moneyRule,
    moneyList: moneyListRule,
};

const routes = [
    {
        path: "/",
        redirect: "/Login",
    },
    {
        path: "/Login",
        name: "Login",
        component: Login,
    },
    {
        path: "/home",
        name: "Home",
        component: Home,
        redirect: "/welcomeHome",
        children: [
            {
                path: "/welcomeHome",
                component: welcomeHome,
            },
        ],
    },
    {
        path: "*",
        component: Not,
    },
];

// const router = new VueRouter({
//   routes
// })
//创建一个创建路由得到函数
const createRouter = (routes) => new VueRouter({ routes });
//在addRoutes的地方使用该函数返回一个路由
const router = createRouter(routes);
//路由全局前置守卫
router.beforeEach((to, form, next) => {
    if (to.path === "/Login") {
        next();
    } else if (!localStorage.token) {
        next("/Login");
    } else {
        next();
    }
});
//解决重复点击报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};
//将该方法暴露，在登路页面方法中引入调用
export function initDynamicRoutes() {
    //根据二级权限对路由进行动态添加
    const currentRoutes = router.options.routes;
    console.log(currentRoutes,'currentRoutes')
    //对二级权限进行遍历，通过vuex拿到权限
    // currentRoutes[2].children.push()
    const rightList = store.state.rightList;
    console.log(rightList);
    rightList.forEach((item) => {
        item.childrenMenu.forEach((i) => {
            const temp = ruleMapping[i.path];
            console.log(temp);
            currentRoutes[2].children.push(temp);
        });
    });
    console.log(currentRoutes);
    //直接add的话，通过地址栏跳转或者刷新页面，路由数据会丢失
    //解决办法。在app.vue中引入该函数
    // router.match = createRouter(routes).match;
    const newRouter=createRouter()
    console.log(newRouter)
    router.matcher=newRouter.matcher
    router.addRoutes(currentRoutes);
}

export default router;
